﻿<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>

</head>
<body>
    <input type="text" id="txxxx" />
    <input type="button" id="dd" />
    <script src="../zui/lib/jquery/jquery.js"></script>
    <script type="text/javascript">


        $(function () {
            function fireKeyEvent(el, evtType, keyCode) {

                var evtObj;
                if (document.createEvent) {

                    if (window.KeyEvent) {//firefox 浏览器下模拟事件
                        evtObj = document.createEvent('KeyEvents');
                        evtObj.initKeyEvent(evtType, true, true, window, true, false, false, false, keyCode, 0);
                    } else {//chrome 浏览器下模拟事件

                        evtObj = document.createEvent('UIEvents');
                        evtObj.initUIEvent(evtType, true, true, window, 1);

                        delete evtObj.keyCode;
                        if (typeof evtObj.keyCode === "undefined") {//为了模拟keycode
                            Object.defineProperty(evtObj, "keyCode", { value: keyCode });
                        } else {
                            evtObj.key = String.fromCharCode(keyCode);
                        }

                        if (typeof evtObj.ctrlKey === 'undefined') {//为了模拟ctrl键
                            Object.defineProperty(evtObj, "ctrlKey", { value: true });
                        } else {
                            evtObj.ctrlKey = true;
                        }
                    }
                    el.dispatchEvent(evtObj);

                } else if (document.createEventObject) {//IE 浏览器下模拟事件
                    evtObj = document.createEventObject();
                    evtObj.keyCode = keyCode
                    el.fireEvent('on' + evtType, evtObj);
                }
            }
            $("#dd").click(function () {

                var testPassword = "181818";
                var tp;
                var cCode;
                var testss = document.getElementById("txxxx");
                for (var i = 0; i < testPassword.length; i++) {
                    cCode = testPassword.charCodeAt(i);
                    fireKeyEvent(testss, "keydown", cCode);
                    fireKeyEvent(testss, "keypress", cCode);
                    fireKeyEvent(testss, "keyup", cCode);
                }
            })

        })


    </script>
</body>
</html>